package com.api.user.service.impl;

import com.api.base.dao.UserMapper;
import com.api.base.model.User;
import com.api.user.dao.IdentificationMapper;
import com.api.user.dao.UserInfoMapper;
import com.api.user.enums.IdentificationStatusEnums;
import com.api.user.model.Identification;
import com.api.user.model.ManageInfo;
import com.api.user.model.UserExamineLog;
import com.api.user.model.UserInfo;
import com.api.user.service.IdentificationService;
import com.api.core.service.AbstractService;
import com.api.user.service.ManageInfoService;
import com.api.user.service.UserExamineLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/03/23.
 */
@Service
@Transactional
public class IdentificationServiceImpl extends AbstractService<Identification> implements IdentificationService {
    @Resource
    private IdentificationMapper identificationMapper;

    @Resource
    private UserExamineLogService userExamineLogService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ManageInfoService manageInfoService;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public Result list(String search, String order, Long uid){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        orderParams.put("create_time","desc");
        params.put("userId",uid);
        List<Map<String, Object>> res = identificationMapper.list(params, orderParams);
        return ResultGenerator.genSuccessResult(res);
    }

    @Override
    public Result add(Identification identification) {
        User user = userMapper.selectByPrimaryKey(identification.getUserId());

        /**
         * 普通用户
         */
        if(user.getType() == 2){
            Identification identification1 = identificationMapper.findByUserType(identification.getUserId(),identification.getType());
//            if(identification1!=null && identification1.getExamine()!=3){
//                return ResultGenerator.genFailResult(400,"已经申请过了");
//            }else
            identification.setExamine(IdentificationStatusEnums.ADOPT);
            identification.setCreateTime(new Date().getTime());
            if(identification1!=null){
                identificationMapper.updateByUserAndType(identification);
            }else{
                this.save(identification);
            }

            UserExamineLog userExamineLog = new UserExamineLog();
            userExamineLog.setAuthenticationId(identification.getUserId());
            userExamineLog.setStatus("1");
            userExamineLog.setFrontUrl(identification.getFrontUrl());
            userExamineLog.setBackUrl(identification.getBackUrl());
            userExamineLog.setAuthenticationType(identification.getType());
            userExamineLogService.save(userExamineLog);
        }
        /**
         * 主持人
         */
        if(user.getType() == 3){
            ManageInfo manageInfo = new ManageInfo();
            manageInfo.setExamine(identification.getExamine());
            manageInfo.setUserId(identification.getUserId());
            manageInfo.setBackUrl(identification.getBackUrl());
            manageInfo.setFrontUrl(identification.getFrontUrl());
            manageInfo.setType(identification.getType());
            return manageInfoService.add(manageInfo);
        }
        /**
         * 完成认证升级为BASE
         */
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(identification.getUserId());
        userInfo.setVipLevel(IdentificationStatusEnums.USER_LEVEL_BASE);
        userInfoMapper.updateByPrimaryKeySelective(userInfo);

        return ResultGenerator.genSuccessResult();
    }
}
